package com.youhome.service.model;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by chen365 on 2019/03/27.
 */
@Data
public class CustomCompanyTokenDetails implements UserDetails {
    private Long tokenId;

    private Long companyId;

    private String tokenName;

    private String tokenValue;

    private Date indate;

    private Date created;

    private Date updated;

    private Boolean enabled;

    private String remark;

    private Set<String> roles;

    private Collection<GrantedAuthority> authorities;

    public CustomCompanyTokenDetails() {
        super();
    }

    public CustomCompanyTokenDetails(CompanyToken companyToken) {
        System.err.println(companyToken.toString());
        this.tokenId = companyToken.getTokenId();
        this.companyId = companyToken.getTokenId();
        this.tokenName = companyToken.getTokenName();
        this.tokenValue = companyToken.getTokenValue();
        this.indate = companyToken.getIndate();
        this.created = companyToken.getCreated();
        this.updated = companyToken.getUpdated();
        this.enabled = companyToken.getEnabled();
        this.remark = companyToken.getRemark();
        this.roles = companyToken.getRoles();
        this.authorities = companyToken.getRoles().stream().map(role -> new SimpleGrantedAuthority(role)).collect(Collectors.toList());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return this.tokenId.toString();
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.enabled;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.enabled;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.enabled;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
